package com.raymond.smgp.session;

import com.raymond.client.codec.BraidedDecoder;
import com.raymond.client.codec.decoding.BaseDecoder;
import com.raymond.client.codec.encodeing.BaseEncoder;
import com.raymond.client.config.ClientSocketConfig;
import com.raymond.client.connector.DefaultClientConnector;
import com.raymond.client.service.RecvHandlerProcessors;
import com.raymond.client.session.AbstractClientSession;
import com.raymond.client.session.AbstractClientSessionManager;
import com.raymond.smgp.config.SmgpClientSocketConfig;
import com.raymond.smgp.enums.CommandIdType;
import com.raymond.smgp.handler.ActiveTestRequestMessageHandler;
import com.raymond.smgp.handler.DeliverRequestMessageHandler;

/**
 * 连接session管理器
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2022-11-01 10:27
 */
public class SmgpClientSessionManager extends AbstractClientSessionManager {
    private BraidedDecoder codec = new BraidedDecoder();
    private RecvHandlerProcessors handlerProcessors = new RecvHandlerProcessors();



    public SmgpClientSessionManager() {
        super(new DefaultClientConnector());
        init();
    }
    private void init() {
        for (CommandIdType value : CommandIdType.values()) {
            BaseDecoder decoder = value.getDecoder();
            if (decoder != null) {
                codec.putBaseDecoder(value.getCommandId(), decoder);
            }
            BaseEncoder encoder = value.getEncoder();
            if (encoder != null) {
                codec.putBaseEncoder(value.getCommandId(), encoder);
            }
        }
        handlerProcessors.putBaseHandler(CommandIdType.DELIVER_REQUEST.getCommandId(), new DeliverRequestMessageHandler());
        handlerProcessors.putBaseHandler(CommandIdType.ACTIVE_TEST_REQUEST.getCommandId(), new ActiveTestRequestMessageHandler());
    }



    @Override
    public AbstractClientSession createSession(ClientSocketConfig socketConfig) {
        return new SmgpClientSession(this, (SmgpClientSocketConfig) socketConfig);
    }


    public BraidedDecoder getCodec() {
        return codec;
    }


    public RecvHandlerProcessors getHandlerProcessors() {
        return handlerProcessors;
    }
}
